﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;
using System.IO;

namespace MarginTrading.Forms
{
    public partial class SettlementFileGen : Form
    {
        public SettlementFileGen()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonBuyGen_Click(object sender, EventArgs e)
        {
            if (ValidateInputs() == false)
            {
                return;
            }
            DataTable dt = null;

            try
            {
                Config c = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);

                if (radioButtonBuy.Checked == true)
                {
                    dt = DailyTransactionsServices.GetSettledDataTable(dateTimePicker.Value, c.Value, "B");
                }
                else
                {
                    dt = DailyTransactionsServices.GetSettledDataTable(dateTimePicker.Value, c.Value, "S");
                }
                StringBuilder clientIDs = new StringBuilder();

                int iCount = dt.Rows.Count;
                if (iCount == 0)
                {
                    MessageBox.Show("No settlements found", "Information", MessageBoxButtons.OK);
                    return;
                }

                for (int iI = 0; iI < iCount; iI++)
                {
                    string clientCode = dt.Rows[iI].ItemArray[0].ToString();// dataGridViewBuy.Rows[iI].Cells[0].Value.ToString();

                    double dAmount = double.Parse(dt.Rows[iI].ItemArray[1].ToString());// dataGridViewBuy.Rows[iI].Cells[5].Value.ToString());

                    string sChequeNO = textBoxCheckNO.Text + "-" + (iI + 1).ToString("D4");

                    String Line = String.Format("{0,-15}{1,-8}{2,-12}{3,-20}",
                        clientCode.Substring(4), DateTime.Today.ToString("ddMMyyyy"), String.Format("{0:0.00}", dAmount), sChequeNO);

                    clientIDs.AppendLine(Line);
                }

                string sFileName = "Settlement_Data" + dateTimePicker.Value.ToLongDateString() + Program.GetNextSeqNO().ToString();

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Text Files|*.txt";
                saveFileDialog1.Title = "Save Settled Selection";
                saveFileDialog1.FileName = sFileName;
                DialogResult result = saveFileDialog1.ShowDialog();

                if (/*saveFileDialog1.FileName != ""*/result == DialogResult.OK)
                {
                    using (StreamWriter outfile = new StreamWriter(saveFileDialog1.FileName))
                    {
                        outfile.Write(clientIDs.ToString());
                        MessageBox.Show("File Saved");
                    }

                    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MarginTrading");
                    Directory.CreateDirectory(path);
                    path = Path.Combine(path, sFileName + ".txt");
                    StreamWriter outfile2 = new StreamWriter(path);

                    using (outfile2)
                    {
                        outfile2.Write(clientIDs.ToString());
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to generate file. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ValidateInputs()
        {
            if (textBoxCheckNO.Text == "")
            {
                MessageBox.Show("Enter cheque number", "Cheque number empty", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
    }
}
